/**
 * 
 */
package com.cipe.cmrs.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cipe.cmrs.model.Donor;
import com.cipe.cmrs.model.User;
import com.cipe.cmrs.util.DateUtil;

/**
 * @author Incepio
 * 
 */

@Service
@Transactional
public class DonorService extends BaseService<Donor> {

	@Autowired
	private UserService userService;
	
	public Donor saveDonor(Donor donor, Integer userId) {
		if(donor != null){
			User user = userService.findById(userId);
			Donor dbDonor = findById(donor.getId());
			if(dbDonor == null){
				dbDonor = new Donor();
				dbDonor.setCreatedBy(user);
				dbDonor.setCreationDate(DateUtil.getCurrentDate());
			}
			dbDonor.setAcronym(donor.getAcronym());
			dbDonor.setName(donor.getName());
			dbDonor.setUrl(donor.getUrl());
			dbDonor.setUpdatedBy(user);
			dbDonor.setUpdationDate(DateUtil.getCurrentDate());
			return merge(dbDonor);
		}
		return null;
	}
	
}
